; text.asm
; Text output routines
;

	.include "common.inc"
	.include "global.inc"
	.include "graphic.inc"

	.export t_regfont
	.export t_font
	.export t_char
	.export t_text
	.export t_drawtext
	.export t_getextent

	.segment "DATA"

; fixed width font variables
cur_ch:		.tag Bitmap
chargen:	.word $8000
ch_pos:		.byte $00
ch_cnt:		.byte $00

; font table
selptrlo:	.byte <(fx_font-1), <(fx_font-1), <(fx_font-1), <(fx_font-1)
			.byte <(fx_font-1), <(fx_font-1), <(fx_font-1), <(fx_font-1) 
selptrhi:	.byte >(fx_font-1), >(fx_font-1), >(fx_font-1), >(fx_font-1)
			.byte >(fx_font-1), >(fx_font-1), >(fx_font-1), >(fx_font-1)
outptrlo:	.byte <fx_char, <fx_char, <fx_char, <fx_char
			.byte <fx_char, <fx_char, <fx_char, <fx_char
outptrhi:	.byte >fx_char, >fx_char, >fx_char, >fx_char
			.byte >fx_char, >fx_char, >fx_char, >fx_char
extptrlo:	.byte <fx_getextent, <fx_getextent, <fx_getextent, <fx_getextent
			.byte <fx_getextent, <fx_getextent, <fx_getextent, <fx_getextent
extptrhi:	.byte >fx_getextent, >fx_getextent, >fx_getextent, >fx_getextent
			.byte >fx_getextent, >fx_getextent, >fx_getextent, >fx_getextent

	.segment "JMPTABLE"

; font selection pointer
choutptr:	.word fx_char
extentptr:	.word fx_getextent

	.segment "CODE"
	
t_regfont:	; A .. font no. (0..7), X/Y .. ptr to font def (sel/out/ext ptr) 
			stx reg2
			sty reg2+1
			and #7
			tax
			ldy #0
			sec
			lda (reg2), y
			sbc #1
			sta selptrlo, x
			iny
			lda (reg2), y
			sbc #0
			sta selptrhi, x
			iny
			lda (reg2), y
			sta outptrlo, x
			iny
			lda (reg2), y
			sta outptrhi, x
			iny
			lda (reg2), y
			sta extptrlo, x
			iny
			lda (reg2), y
			sta extptrhi, x
			rts			
	
t_font:		; A .. font (0..7)
			and #7
			tax
			lda outptrlo, x
			sta choutptr
			lda outptrhi, x
			sta choutptr+1
			lda extptrlo, x
			sta extentptr
			lda extptrhi, x
			sta extentptr+1
			lda selptrhi, x
			pha
			lda selptrlo, x
			pha
			txa
			rts

fx_font:	and #1
			asl
			asl
			asl
			clc
			adc #$80
			sta chargen+1
			lda #8
			sta cur_ch+Bitmap::height
			sta cur_ch+Bitmap::width
			rts
	
t_char:		; A ... char code
			jmp (choutptr)
fx_char:	sta cur_ch+Bitmap::buf_ptr
			lda #0
			ldx #3			; code mul 8
@tc_mull:	asl cur_ch+Bitmap::buf_ptr
			rol a
			dex
			bne @tc_mull
			sta cur_ch+Bitmap::buf_ptr+1
			clc				; add base ptr
			lda chargen
			adc cur_ch+Bitmap::buf_ptr
			sta cur_ch+Bitmap::buf_ptr
			lda chargen+1
			adc cur_ch+Bitmap::buf_ptr+1
			sta cur_ch+Bitmap::buf_ptr+1
			lda #<cur_ch	; output char
			ldx #>cur_ch
			jmp g_putbm
	
t_text:		; A/X ... text adr, Y .. text len
			sta reg2
			stx reg2+1
			sty ch_cnt
			ldy #0
			sty ch_pos
putchar:	lda (reg2), y
			jsr t_char
			inc ch_pos
			ldy ch_pos
			cpy ch_cnt
			bne putchar
			rts

t_drawtext:	; A/X ... text adr (first byte len)
			sta reg2
			stx reg2+1
			ldy #0
			lda (reg2), y
			beq @fin
			sta ch_cnt
			inc ch_cnt
			beq @drawbm
			iny
			sty ch_pos
			bne putchar
@drawbm:	jsr getbmptr
			jmp g_putbm
@fin:		rts

getbmptr:	ldy #2
			lda (reg2), y	; hi byte Bitmap
			tax
			dey
			lda (reg2), y	; lo byte Bitmap
			rts

t_getextent:; A/X .. text adr (first byte len) -> X/Y text extent
			sta reg2
			stx reg2+1
			ldy #0
			lda (reg2), y
			cmp #$FF
			bne @fntout
			jsr getbmptr
			sta reg2
			stx reg2+1
			ldy #0
			lda (reg2), y
			tax
			iny
			lda (reg2), y
			tay
			rts
@fntout:	jmp (extentptr)
fx_getextent:
			tax
			beq @nowidth
			lda #0
			clc
@calcwidth:	; fixed width font!
			adc cur_ch+Bitmap::width
			dex
			bne @calcwidth
			tax
@nowidth:	ldy cur_ch+Bitmap::height
			rts

			